import { ref, reactive } from "vue";
import { getDictOptions } from "@/services/api/system/dict";

interface OptionProps {
  label: string;
  value: number | string;
  text: number | string;
  [key: string]: any;
}

interface Options {
  [key: string]: OptionProps[];
}
/**
 * @description 切换主题
 * */
export const useOptions = (props: { names: string[] }) => {
  const { names = [] } = props;

  const opts: Options = {};
  names.forEach((item: string) => {
    opts[item] = [];
  });

  const options = ref<Options>(opts);

  getDictOptions({
    names: names.join(","),
  }).then(({ data }) => {
    data.forEach((element: OptionProps) => {
      opts[element.name] = element.options.map((item: { value: any }) => {
        return {
          ...item,
          value:
            element.valueType === "number" ? Number(item.value) : item.value,
        };
      });
    });
    options.value = {
      ...options.value,
      ...opts,
    };
  });

  return {
    options: reactive(options),
  };
};
